<template>
  <dt-modal-edit ref="edit" :title="title" :width="500" :loading="loading" @ok="onOk"
    @cancelData="hide">
    <div>
      <div style="margin-bottom: 10px;" v-if="receiverFlag=='2'">
        <p v-if="this.objData.source=='1'|| (this.objData.source=='3'&&!this.clueFlag)">
          分配线索后，勾选线索可在分配人员的未到访客户列表查看该线索信息
        </p>
        <p v-if="this.objData.source=='2'|| (this.objData.source=='3'&&this.clueFlag)">
          请选择分入客户的成员。分配后，该成员可在“到访客户”中查看客户信息</p>
      </div>

      <p style="margin-bottom: 10px;text-align:center;color:#ff9900" v-else>
        当前线索来自于同策好房，只能分配给案场承接人</p>

      <Input v-model="searchVal" prefix='ios-search' @on-change='searchConsultant'
        placeholder="输入人员姓名、手机号或工号搜索">
      </Input>
      <div class="radioGroupClassBox">
        <RadioGroup v-model="chooseConsultant" vertical class="radioGroupClass"
          @on-change='raidoChange'>
          <Radio v-for="(item,index) in consultantList" :key="index" :label="item.userId"
            class="radioClass">
            {{item.userName}}({{item.workNo}}) <Tag>
              {{(item.roleList&&item.roleList.length>0)?item.roleList[0].roleName:''}}</Tag>
          </Radio>
        </RadioGroup>
      </div>
    </div>
  </dt-modal-edit>
</template>

<script>
  import columnOpt from '../opt/allotModalColumn'
  import clueCustomerMgr from '@/biz/customer/clueCustomer.js'
  import publicCustomerMgr from '@/biz/customer/publicCustomer.js'
  export default {
    data() {
      return {
        columns: [],

        loading: false,
        userCustomerId: null,
        distributionWay: '',

        receiverFlag: null,
        publicVal: '',

        // 3.7
        userName: null,
        workNo: null,
        consultantList: null,
        chooseConsultant: null,
        searchVal: '',

        privacyFlag: null,
        objData: null,
        sourceText: null,
        timer: null,
        title: '',
        clueFlag: false,
      }
    },
    methods: {
      raidoChange(val) {
        console.log(val, 'vvvvvvvv');
        let current = this.consultantList.find(item => {
          return item.userId == val

        })
        console.log(current, 'current');
        if (current) {
          this.userName = current.userName
          this.workNo = current.workNo
        }
        console.log(val, this.userName, this.workNo, 'choose');
      },
      onOk() {
        if (!this.chooseConsultant) {
          this.$Message.error('title')
          return
        }
        if (this.objData.source == 1) {
          let params = {}
          if (this.objData.assignAll) {
            params = {
              operationType: 1,
              buildingId: this.$dt.buildInfo().housesId,
              // userCustomerIdList: this.objData.userCustomerId,
              targetUserId: this.chooseConsultant,
              ...this.objData.screening
            }
          } else {
            params = {
              operationType: 0,
              buildingId: this.$dt.buildInfo().housesId,
              userCustomerIdList: this.objData.userCustomerId,
              targetUserId: this.chooseConsultant
            }
          }

          this.$Modal.confirm({
            title: `分配${this.sourceText}客户确认`,
            content: `确定将${this.objData.total?this.objData.total:this.objData.userCustomerId.length}个客户分配给${this.userName}(${this.workNo})？`,
            closable: true,
            onOk: () => {
              clueCustomerMgr.clueDistributeClue(params).then(res => {
                this.$Message.success('分配成功。')
                this.$refs.edit.hide()
                this.hide()
                this.$emit('assignSuccess')
              })

            }
          })
        } else if (this.objData.source == 2) {
          let params = {}
          if (this.objData.assignAll) {
            params = {
              operationType: 1,
              buildingId: this.$dt.buildInfo().housesId,
              // userCustomerIdList: this.objData.userCustomerId,
              newUserId: this.chooseConsultant,
              ...this.objData.screening
            }
          } else {
            params = {
              operationType: 0,
              buildingId: this.$dt.buildInfo().housesId,
              customerIds: this.objData.userCustomerId,
              newUserId: this.chooseConsultant
            }
          }
          this.$Modal.confirm({
            title: `分配${this.sourceText}客户确认`,
            content: `确定将${this.objData.total?this.objData.total:this.objData.userCustomerId.length}个客户分配给${this.userName}(${this.workNo})？`,
            closable: true,
            onOk: () => {
              clueCustomerMgr.visitDistribute(params).then(res => {
                this.$Message.success('分配成功。')
                this.$refs.edit.hide()
                this.hide()
                this.$emit('assignSuccess')
              })
            }
          })

        } else if (this.objData.source == 3) {
          let params = {}
          if (this.objData.assignAll) {
            params = {
              operationType: 1,
              buildingId: this.$dt.buildInfo().housesId,
              // userCustomerIdList: this.objData.userCustomerId,
              targetUserId: this.chooseConsultant,
              ...this.objData.screening
            }
          } else {
            params = {
              operationType: 0,
              buildingId: this.$dt.buildInfo().housesId,
              publicPoolIdList: this.objData.userCustomerId,
              targetUserId: this.chooseConsultant
            }
          }

          this.$Modal.confirm({
            title: `分配${this.sourceText}客户确认`,
            content: `确定将${this.objData.total?this.objData.total:this.objData.userCustomerId.length}个客户分配给${this.userName}(${this.workNo})？`,
            closable: true,
            onOk: () => {
              if (!this.clueFlag) {
                publicCustomerMgr.distributePublicClue(params).then(res => {
                  this.$Message.success('分配成功。')
                  this.$refs.edit.hide()
                  this.hide()
                  this.$emit('assignSuccess')
                })
              } else {
                publicCustomerMgr.distributePublicCustomerVisit(params).then(res => {
                  this.$Message.success('分配成功。')
                  this.$refs.edit.hide()
                  this.hide()
                  this.$emit('assignSuccess')
                })
              }
            }
          })

        }
      },
      searchConsultant(val) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          this.onFetch()
        }, 500)
      },
      onFetch() {
        let params = {
          adviserKeyword: this.searchVal ? this.searchVal : '',
          buildingId: this.$dt.buildInfo().housesId,
          onlineType: 1,
          privacy:this.objData.privacyFlag?this.objData.privacyFlag: this.objData.showTab == 1 ? 1 : 0,
          haveRole: this.clueFlag ? 3 : 1 //此处待更改有的不止是顾问
        }
        clueCustomerMgr.consultantList(params).then((res) => {
          this.consultantList = res
        })

      },
      show(obj) {
        console.log(obj,'vvvvvvvvv');
        this.objData = obj
        if(this.objData.privacyFlag===1){
          console.log(12);
          this.receiverFlag = 1
        }else if(this.objData.privacyFlag===0){
          this.receiverFlag = 2
        }else{
          console.log(13);
          this.receiverFlag = this.objData.showTab
        }
         console.log(this.receiverFlag ,'vvvvvvvvv');

        // // this.privacyFlag = privacy //是否隐私号 1 隐私号  2 非隐私号
        // // this.source = source // 父级  1 线索 2 到访 3公客池
        // // this.userCustomerId = val //选中的客户

        if (this.objData.source == '1') {
          this.clueFlag = false
          this.sourceText = '线索'
          this.title = '请选择需分入线索的人员'
        } else if (this.objData.source == '2') {
          this.clueFlag = true
          this.sourceText = '到访'
          this.title = '请选择分入到访客户的人员 '

        } else if (this.objData.source == '3') {
          this.sourceText = '公客池'
          this.title = '请选择分入公客池客户的人员'
          if (this.objData.topNum && (this.objData.topNum == '3' || this.objData
              .topNum == '5')) {
            this.clueFlag = false
          } else {
            this.clueFlag = true
          }

        }
        console.log(this.clueFlag, 'this.sourceText');
        this.$refs.edit.show()
        this.onFetch()
      },
      hide() {
        this.chooseConsultant = null
        this.searchVal = ''

      },
    }
  }
</script>

<style lang="scss" scoped>
  .radioGroupClassBox {
    max-height: 500px;
    overflow-y: auto;
    margin-top: 10px;
  }

  .radioGroupClass {
    display: block;
    width: 100%;
  }

  .radioClass {
    border-bottom: 1px solid #e8eaec;
    display: block;
    padding: 10px 10px 40px;
  }
</style>